package cn.tedu.mvcaddress.controller;

import cn.tedu.mvcaddress.mapper.AddressMapper;
import cn.tedu.mvcaddress.pojo.entity.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
public class AddressController {

    @Autowired
    private AddressMapper addressMapper;

    @RequestMapping("/address/insert")
    @ResponseBody
    public String insert(Address address) {
        addressMapper.insert(address);
        return "添加收货地址成功!<a href='/index.html'>返回首页</a>";
    }

    @RequestMapping("/address/select")
    @ResponseBody
    public String select() {
        List<Address> list = addressMapper.select();
        String html = "<table border='1'>";
        html += "<caption>地址列表</caption>";
        html += "<tr><th>id</th><th>收件人</th><th>收件地址</th><th>邮箱</th><th>手机号</th><th>标签</th><th>操作</th></tr>";
        for (Address a : list) {
            html += "<tr>";
            html += "<td>" + a.getId() + "</td>";
            html += "<td>" + a.getReceiver() + "</td>";
            html += "<td>" + a.getAddress() + "</td>";
            html += "<td>" + a.getEmail() + "</td>";
            html += "<td>" + a.getMobile() + "</td>";
            html += "<td>" + a.getTag() + "</td>";
            html += "<td><a href='/address/delete?id=" + a.getId() + "'>删除</a></td>";
            html += "</tr>";
        }
        html += "</table><a href='/'>返回首页</a>";
        return html;
    }

    @RequestMapping("/address/delete")
    @ResponseBody
    public String delete() {
        addressMapper.delete(1);
        return "删除成功!<a href='/address/select'>返回列表页面</a>";
    }

    @RequestMapping("/address/update")
    @ResponseBody
    public String update(Address address) {
        addressMapper.update(address);
        return "修改成功!<a href='/address/select'>返回列表页面</a>";
    }
}
